"0","subsample.pool <- function(df){

# make a list in which we will store our results
  
subsamps <- list()

# now loop through each number in the set 1:100 and draw individuals from the vector of species IDs (letters) based on the vector of sampling probabilites.  We start with a sample of 1, then sample another individual for a total sample size of 2, then 2 more individuals for a total sample size of 4, then 4 more individuals for a total sample size of 8, etc. up to a sample size of 128 individuals

for (i in 1:100){
  
# here we sample indivuals, using the \"sample()\" function: 
  
                samp.1 <- as.factor(sample(species,1,prob = probs, replace =TRUE))
                samp.2 <- c(samp.1,as.factor(sample(s.probs$species,1,prob = s.probs$probs, replace =TRUE)))
                samp.4 <- c(samp.2,as.factor(sample(s.probs$species,2,prob = s.probs$probs, replace =TRUE)))
                samp.8 <- c(samp.4,as.factor(sample(s.probs$species,4,prob = s.probs$probs, replace =TRUE)))
                samp.16 <- c(samp.8,as.factor(sample(s.probs$species,8,prob = s.probs$probs, replace =TRUE)))
                samp.32 <- c(samp.16,as.factor(sample(s.probs$species,16,prob = s.probs$probs, replace =TRUE)))
                samp.64 <- c(samp.32,as.factor(sample(s.probs$species,32,prob = s.probs$probs, replace =TRUE)))
                samp.128 <- c(samp.64,as.factor(sample(s.probs$species,64,prob = s.probs$probs, replace =TRUE)))
                
# now we use the \"unique()\" function to determine how many species are in each subsample:
                
                rich.1 <- length(unique(samp.1))
                rich.2 <- length(unique(samp.2))
                rich.4 <- length(unique(samp.4))
                rich.8 <- length(unique(samp.8))
                rich.16 <- length(unique(samp.16))
                rich.32 <- length(unique(samp.32))
                rich.64 <- length(unique(samp.64))
                rich.128 <- length(unique(samp.128))
                
# finally we'll combine these outputs into a data frame.  We'll also include the nunber of individuals sampled and the iteration number so that we can use these for making plots.
                
                N <- c(1,2,4,8,16,32,64,128)
                S <- c(rich.1,rich.2,rich.4,rich.8,rich.16,rich.32,rich.64,rich.128)
                iteration <- rep(as.factor(i),8)
                out <- data.frame(iteration,N,S)
                subsamps[[i]] <- out
                }

# the loop outputs a list of 100 data frames like the one we generated above.  To make the output easier to work with, we'll combine them into a single data frame.  We use the \"do.call\" function to apply an operation -binding by shared rows, \"rbind()\" - to the list of data frames output by the loop.

subsamples <- do.call(\"rbind\",subsamps) 
return(subsamples)
}"
